package com.zyzy.leftfourdead.adapter.fruitadapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zyzy.leftfourdead.R;
import com.zyzy.leftfourdead.bean.FruitBean;
import com.zyzy.leftfourdead.helper.MyListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/21.
 * 这是 水产五折 下方商品的适配器
 */
public class FruitFragmentAdapter extends BaseAdapter {
    private static final String TAG = "totol";
    //    上下文对象
    public Context context = null;
    //    list集合
    public List<FruitBean.ProductsBean> list = null;
    //    布局填充器
    public LayoutInflater inflater = null;
    //    list集合
    public List<List<FruitBean.ProductsBean>> totalList = new ArrayList<>();

    public List<FruitBean.ProductsBean> titleList=new ArrayList<>();

    public FruitFragmentAdapter(Context context, List<FruitBean.ProductsBean> list) {
        this.context = context;
        this.list = list;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    private void initData() {
//        标题集合
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getType().equals("group")) {
                titleList.add(list.get(i));
                Log.i(TAG, "+++++====groupName: "+list.get(i).getName());
            }
        }
//        分类产品集合
        for (int i = 0; i < titleList.size(); i++) {
            List<FruitBean.ProductsBean> beanList=new ArrayList<>();
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).getType().equals("product") && list.get(j).getProduct_group_name().equals(titleList.get(i).getName())) {
                    beanList.add(list.get(j));
                    Log.i(TAG, "+++++====productName: "+list.get(j).getName());
                }
            }
            totalList.add(beanList);
        }

    }

    /**
     * Gson 解析
     *
     * @param jsonString
     * @return
     */
    public FruitBean parseJsonToWaterBean(String jsonString) {
        Gson gson = new Gson();
        FruitBean bean = gson.fromJson(jsonString, new TypeToken<FruitBean>() {
        }.getType());
        return bean;
    }

    //    刷新list
    public void reloadListView(List<FruitBean.ProductsBean> data, boolean isClear) {
        if (isClear) {
            list.clear();
            totalList.clear();
            titleList.clear();
        }
        list.addAll(data);
        initData();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {

        return totalList.size();
    }

    @Override
    public Object getItem(int position) {
        return totalList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Log.i(TAG, "+++++====initData: ");
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_fragment_water_listview, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.textView_water_listView_item.setText(titleList.get(position).getName());
        final String imageUrl = titleList.get(position).getRight_image();
        if (imageUrl != null) {
            Glide.with(context).load(imageUrl).into(viewHolder.imageView_water_listView_item);
//            viewHolder.imageView_water_listView_item.setImageURI(Uri.parse(imageUrl));
        }
        FruitItemFragmentAdapter adapter = new FruitItemFragmentAdapter(context, totalList.get(position));
        viewHolder.listView_water_listView_item.setAdapter(adapter);
        Log.i(TAG, "!!!!!!!list: "+totalList.toString());
        return convertView;
    }

    class ViewHolder {
        private MyListView listView_water_listView_item;
        private TextView textView_water_listView_item;
        private ImageView imageView_water_listView_item;

        public ViewHolder(View convertView) {
            listView_water_listView_item = (MyListView) convertView.findViewById(R.id.listView_water_listView_item);
            textView_water_listView_item = (TextView) convertView.findViewById(R.id.textView_water_listView_item);
            imageView_water_listView_item = (ImageView) convertView.findViewById(R.id.imageView_water_listView_item);
        }
    }
}
